const app = getApp()
Page({
  data: {
    boxId:0,
    box_list:[],
    current_box_list:[],
    currentBoxSum:0,
      currentBoxes:false,
      currentBoxId:0,
      currentBoxIndex:0,
      showDetail:false,
      pageNum:1,
      introduceImgs: []
  },
  onLoad: function (options) {
    if(options.id){
      this.setData({
        boxId:options.id
      })
    }
    this.setData({
      box_icon:app.box_icon
    })
    wx.showShareMenu({
      withShareTicket: true,
    })
  },
  onShareTimeline() {
    return {
      title: '梦与玩',
      query: `/pages/boxes/blind-boxes?id=${this.data.boxId}`
    }
  },
  onShareAppMessage() {

  },
  // 获取商品详情
  getBoxDetail() {
    let _this = this;
    app.util.request({
      url: "api/box/findById",
      method: "post",
      data: {
        id:_this.data.currentBoxId
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            introduceImgs: response.data.data.introduce.split(',')
          })
          console.log(_this.introduceImgs)
          // var WxParse = require('../../utils/wxParse/wxParse.js');
          // WxParse.wxParse('article', 'html', response.data.data.introduce, _this, 10);
          // _this.setData({
          //   boxDetail: response.data.data
          //   // showDetail: false
          // })
        }
      }
    })
  },
  openbox(e){
    this.setData({
      currentBoxes:true,
      currentBoxId:e.currentTarget.dataset.id,
      currentBoxIndex:e.currentTarget.dataset.index
    })
    this.getCurrentBoxList()
  },
  changeCurrent(e){
    let type = e.currentTarget.dataset.bool;
    this.setData({
      currentBoxes: type == '1' ? true: false,
    })
  },
  toLastBox(){
    if (this.data.currentBoxIndex) {
      for (let i=this.data.currentBoxIndex-1;i>=-1;i--){
        if (this.data.box_list[i].surplus!=0){
          this.setData({
            currentBoxId:this.data.box_list[i].id,
            currentBoxIndex:i
          })
          this.getCurrentBoxList()
          this.getBoxDetail()
          break;
        }else{
          wx.showToast({
            icon:'none',
            title:'前面没有了'
          })
        }
      }
    }else{
      wx.showToast({
        icon:'none',
        title:'前面没有了'
      })
    }
  },
  toNextBox(){
    if (this.data.currentBoxIndex<this.data.box_list.length-1) {
    for (let i=this.data.currentBoxIndex+1;i<this.data.box_list.length;i++){
      if (this.data.box_list[i].surplus!=0){
        this.setData({
          currentBoxId:this.data.box_list[i].id,
          currentBoxIndex:i
        })
        this.getCurrentBoxList()
        this.getBoxDetail()
        break;
      }else{
        wx.showToast({
          icon:'none',
          title:'最后一个了'
        })
      }
    }
    }else {
      wx.showToast({
        icon:'none',
        title:'最后一个了'
      })
    }
  },
  getBoxList(){
    let _this = this;
    app.util.request({
        url: "api/box/findLimitAll",
        method: "get",
        data: {
          seriesId:_this.data.boxId,
          page: _this.data.pageNum,
          pageSize:16,
          name: "",
          type:1
      },
        success(response) {
            if (response.data.code == 0) {
                _this.setData({
                   box_list: response.data.data.list
                })
                if (_this.data.box_list.length >= 1) {
                  _this.setData({
                    currentBoxId: _this.data.box_list[0].id,
                    currentBoxes: true
                  })
                  _this.getCurrentBoxList()
                  _this.getBoxDetail()
                } else {
                  _this.setData({
                    currentBoxes: false
                  })
                }
                if(_this.data.pageNum<response.data.data.pages){
                  _this.data.pageNum++
                  _this.getBoxList()
                }
            }
        }
    })
  },
  getCurrentBoxList(){
    let _this = this;
    _this.setData({
      current_box_list:[]
    })
    wx.showLoading({
      title: '加载中',
    })
    app.util.request({
        url: "api/etui/findALLByBoxId",
        method: "get",
        data: {
          boxId:_this.data.currentBoxId
      },
        success(response) {
            if (response.data.code == 0){
                _this.setData({
                   current_box_list: response.data.data[0].etui,
                   currentBoxSum:response.data.data[0].surplus
                });
            }
            wx.hideLoading()
        }
    })
  },
  // 点击某一个具体的盒子进入订单页
  toChooseBoxOrder(e){
    wx.navigateTo({
      url: '/pages/order/create-order/create-order?boxId='+e.currentTarget.dataset.id+'&number=1&type=1&price='+this.data.box_list[this.data.currentBoxIndex].price+"&img="+e.currentTarget.dataset.item.img+"&name="+e.currentTarget.dataset.item.name,
    })
  },
  showBoxDetail(){
    let _this = this;
    // app.util.request({
    //   url: "api/box/findById",
    //   method: "post",
    //   data: {
    //     id:_this.data.currentBoxId
    //   },
    //   success(response) {
    //     if (response.data.code == 0) {
    //       let content = response.data.data.introduce;
    //       var WxParse = require('../../utils/wxParse/wxParse.js');
    //       WxParse.wxParse('article', 'html', content, _this, 10);
          _this.getBoxDetail()
          _this.setData({
            showDetail:true
          })
    //     }
    //   }
    // })
  },
    toBoxOrder(e){
      let _this = this;
      console.log(e, '111')
      let number=e.currentTarget.dataset.num?e.currentTarget.dataset.num:_this.data.currentBoxSum;
      console.log(_this.data.currentBoxSum)
      if(_this.data.currentBoxSum==0){
        wx.showToast({
          icon:'none',
          title: '盒子已经卖完啦',
        })
      }else{
        wx.navigateTo({
          url: '/pages/order/create-order/create-order?id='+this.data.currentBoxId+'&number='+ number+'&type=1',
        })
      }
    },
    onClose(){
      this.setData({
        showDetail:false
      })
    },
  back(){
    // if(this.data.currentBoxes)
    //   this.changeCurrent()
    // else
      wx.navigateBack({
        delta: 1
      })
  },
  onShow: function () {
    this.getBoxList()
    this.getCurrentBoxList()
  },
})
